package com.uflower.order.config;

import com.rabbitmq.client.Channel;
import com.uflower.order.entity.OrderEntity;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyRabbitmqConfig {
//    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
//        System.out.println("收到过期的订单消息，准备关闭订单"+orderEntity.getOrderSn());
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }
    @Bean
    public MessageConverter messageConverter(){
        return  new Jackson2JsonMessageConverter();
    }
    @Bean
    public Queue orderDelayQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete
        Map<String,Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange","order-event-exchange");
        args.put("x-dead-letter-routing-key","order.release.order");
        args.put("x-message-ttl",60000);//1分钟
        return new Queue("order.delay.queue",true,false,false,args);
    }
    @Bean
    public Queue orderReleaseOrderQueue(){
        return new Queue("order.release.queue",true,false,false);
    }
    @Bean
    public Exchange orderEventExchange(){
        //String name, boolean durable, boolean autoDelete
         return   new TopicExchange("order-event-exchange",true,false );
    }
    @Bean
    public Binding OrderCreateOrderBinding(){
        return  new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }
    @Bean
    public Binding OrderReleaseOrderBinding(){
        return  new Binding("order.release.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }
    /**
     * 订单释放和库存释放进行绑定
     *
     */
    @Bean
    public Binding orderReleaseOtherBinding(){
        return  new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }
}
